// components/user-selector/index.ts
import { debugPrint } from 'XrFrame/kanata/lib/index';
import {getTagsArray} from '../../api/iotapi/tag'
import { LENGYU_SUCCESS_CODE, PageRequestParam } from '../../utils/request';
Component({

  /**
   * 组件的属性列表
   */
  properties: {
    show: { // 属性名
      type: Boolean,
      value: false
    },
  },
  observers:{
    show:function(value){
    
      if(value){
        this.setData({
          tagOptionMap:{},
          selectList:[],
          currentOptions:[],
          levelList:[{"id":"first","name":"全部"}]
        })
        console.log(this.data.tagList )
        if(!this.data.tagList || this.data.tagList.length == 0){
          this.getTagList();
        }
      }


    },
    initValue:function(){
    }
  },
  pageLifetimes: {
    // 组件所在页面的生命周期函数
    show: function () {

     },
    hide: function () { },
    resize: function () { 

    },
  },
  /**
   * 组件的初始数据
   */
  data: {
    productId:"8cb31f7297753c0c711a586fe4711855",
    tagList:[],
    tagOptionMap:{},
    selectList:[],
    currentOptions:[],
    levelList:[{"id":"first","name":"全部"}],

  },

  /**
   * 组件的方法列表
   */
  methods: {
    closeDialog(){
      this.setData(
        {

          'tagList':[]
        }
      )
      this.setData({
          'show':false
      })
      this.triggerEvent('dialogClosed') 
    },
    async getTagList(){
      let result = await getTagsArray(this.data.productId);
      console.log(result);
      this.setData({
        'tagList':result.data
      })
      if(result.data){
        this.setData({
          'tagOptionMap.first':result.data,
          'currentOptions':result.data
        })
        this.traverseJson(result.data)
      }
    },
    traverseJson(obj:any){
      obj.forEach((item:any) => {
          let key1:string =  "tagOptionMap."+item.id;
          if(item.children){
            this.setData({
              [key1]:item.children
            })
            this.traverseJson(item.children)
          }
      } )
      console.log("tagmap",this.data.tagOptionMap)
    },
    checkboxChange(e:any){
      console.log("分组选择",e)
      let selectedFlag = false
      let tagId = e.currentTarget.dataset.id
      let tagName = e.currentTarget.dataset.name
      if(e.detail.value == e.currentTarget.dataset.id){
        selectedFlag = true;
      }
      this.data.currentOptions.forEach((item:any)=>{
        if(item.id==e.currentTarget.dataset.id){
           item.checked = selectedFlag
        }
      })
      let newSelectList =[]
      if(selectedFlag){
        newSelectList = this.data.selectList;
        newSelectList.push({
          id:tagId,
          name:tagName
        })
      }
      else{
        this.data.selectList.forEach((item)=>{
          if(item.id!=tagId){
            newSelectList.push(item);
          }
        })
      }
      this.setData({
        selectList:newSelectList
      })
    
    },
    levelChange(e:any){
      let tagId:string =  e.currentTarget.dataset.id
      console.log(tagId)
      let newLevelList = []
      for (const item of this.data.levelList) {
        if(item.id==tagId){
            newLevelList.push(item)
            break;
        }
        else{
          newLevelList.push(item)
          console.log("继续执行")
        }
      }
      if(newLevelList.length == this.data.levelList.length){
        return;
      }
      console.log("变更之后的导航",newLevelList)
        // 更新视图层
      this.setData({ levelList: newLevelList });
      if (this.data.tagOptionMap.hasOwnProperty(tagId)) {
        this.setData({
          currentOptions:this.data.tagOptionMap[tagId]
        })
    
      } 
      else{
        console.log("不含下一层")
      }
    },
    nextLevel(e:any){
      let tagId:string =  e.currentTarget.dataset.id
      console.log(tagId)
      if (this.data.tagOptionMap.hasOwnProperty(tagId)) {
        console.log("包含下一层")
        this.setData({
          currentOptions:this.data.tagOptionMap[tagId]
        })
        let level:any={
          id:tagId,
          name:e.currentTarget.dataset.name
        }
        this.data.levelList.push(level);
        // 更新视图层
        this.setData({ levelList: this.data.levelList });
      } 
      else{
        console.log("不含下一层")
      }
     
    },
    clearData(){
      this.setData(
        {
          'pageParam.paramMapObj.vx_nick_name':''
        }
      )
    },
   
    confirmTag(e:any){
      //提交数据到父页面
      this.triggerEvent('selectorChanged', {value:this.data.selectList}) 
      this.closeDialog();
    },
    searchChanged(e:any){
      this.setData(
        {
          'pageParam.paramMapObj.vx_nick_name':e.detail.value
        }
      )
    },
    imageError(e:any){
      console.log("图片加载失败",e)

    }

  }
})